package com.ltv.xiangqibook.web;

import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Properties;

public class XiangQiBookConfiguration {
    public static final String CONFIG_FILE_NAME = "conf/xiangqibook.conf";
    
    private static final String PLUGIN_VERSION = "plugin_version";
    private static final String XML_VERSION = "xml_version";
    private static final String IS_FIRST_LOAD = "is_first_load";
    
    private Properties props;
    
    private static XiangQiBookConfiguration instance;
    
    private XiangQiBookConfiguration() {
        props = new Properties();
    }

    public static XiangQiBookConfiguration getInstance() {
        if (instance == null) {
            instance = new XiangQiBookConfiguration();
        }
        return instance;
    }
    
    public String getPluginVersion() {
        return props.getProperty(PLUGIN_VERSION);
    }
    
    public String getXmlVersion() {
        return props.getProperty(XML_VERSION);
    }
    
    public boolean isFirstLoad() {
    	try {
			return Boolean.parseBoolean(props.getProperty(IS_FIRST_LOAD));
		} catch (Exception e) {
			return false;
		}
    }
    
    public void loadConfiguration(String fileName) throws Exception {
        FileInputStream fi = null;
        InputStreamReader isr = null;
        try {
            fi = new FileInputStream(fileName);
            isr = new InputStreamReader(fi, "utf-8");
            props.load(isr);
        } finally {
            if (fi != null) {
                fi.close();
            }
            if (isr != null) {
                isr.close();
            }
        }
    }
    
}
